#!/bin/bash
set -e

if [ $# -eq 2 -a "x${2}" = "xsubbuild" ]; then
	echo "配置环境变量: [已配置]"
else
	echo "配置环境变量: [开始]"
	#>>-----------------------------------------------------------------------------------
	#切换目录
	cd ../
	#-------------------------------------------------------------------------------------

	#>>-----------------------------------------------------------------------------------
	#顶层项目目录
	export PROJECT_TOPDIR=$PWD
	#顶层当前配置文件
	export CURCFG_FILE=${PROJECT_TOPDIR}/.curconfig.sh
	#-------------------------------------------------------------------------------------

	#>>env--------------------------------------------------------------------------------
	#设置颜色函数
	source ${PROJECT_TOPDIR}/a.OEMKIT/scripts/dugi.000.colorsetup.sh > /dev/null
	#导入公共变量
	source ${PROJECT_TOPDIR}/a.OEMKIT/scripts/dugi.001.envsetup_common.sh > /dev/null
	#导入平台变量
	source ${PROJECT_TOPDIR}/a.OEMKIT/scripts/dugi.002.envsetup_platform.sh > /dev/null
	#-------------------------------------------------------------------------------------

	#>>-----------------------------------------------------------------------------------
	#切回目录
	cd -> /dev/null
	#-------------------------------------------------------------------------------------
	echo "配置环境变量: [完毕]"
fi

function total_build_module()
{
	pinfo "批量构建... [开始]"
	for file in $(ls *.midkit.*.sh)
	do
		pinfo "执行文件: $file [开始]"
		. ./$file build > /dev/null
		if [ $? -eq 0 ]; then
			pdone "执行文件: $file [完毕]"
		else
			perro "执行文件: $file [失败]"
		fi
	done
	pdone "批量构建... [结束]"
}
function total_clean_module()
{
	pinfo "批量清除... [开始]"
	for file in $(ls *.midkit.*.sh)
	do
		pinfo "执行文件: $file [开始]"
		. ./$file clean > /dev/null
		if [ $? -eq 0 ]; then
			pdone "执行文件: $file [完毕]"
		else
			perro "执行文件: $file [失败]"
		fi
	done
	pdone "批量清除... [结束]"
}
function total_pack_module()
{
	pinfo "批量打包... [开始]"
	for file in $(ls *.midkit.*.sh)
	do
		pinfo "执行文件: $file [开始]"
		. ./$file pack
		if [ $? -eq 0 ]; then
			pdone "执行文件: $file [完毕]"
		else
			perro "执行文件: $file [失败]"
		fi
	done
	pdone "批量打包... [结束]"
}

function total_install_module()
{
	return 0
	pinfo "安装midkit文件... [开始]"
	sudo cp ${MIDKIT_TOPDIR}/out/z.output.lrzsz/bin/lrz                 ${PROJECT_TOPDIR}/z.src.rtfs/mbin/
	sudo cp ${MIDKIT_TOPDIR}/out/z.output.lrzsz/bin/lsz                 ${PROJECT_TOPDIR}/z.src.rtfs/mbin/
	sudo cp ${MIDKIT_TOPDIR}/out/z.output.mtdutils/usr/sbin/ubiformat   ${PROJECT_TOPDIR}/z.src.rtfs/mbin/
	sudo cp ${MIDKIT_TOPDIR}/out/z.output.mtdutils/usr/sbin/nandwrite   ${PROJECT_TOPDIR}/z.src.rtfs/mbin/
	sudo cp ${MIDKIT_TOPDIR}/out/z.output.mtdutils/usr/sbin/flash_erase ${PROJECT_TOPDIR}/z.src.rtfs/mbin/
	sudo cp ${MIDKIT_TOPDIR}/out/z.output.vsftpd/usr/sbin/vsftpd        ${PROJECT_TOPDIR}/z.src.rtfs/mbin/
	sudo cp ${MIDKIT_TOPDIR}/out/z.output.vsftpd/etc/vsftpd.conf        ${PROJECT_TOPDIR}/z.src.rtfs/etc/vsftpd.conf
	pdone "安装midkit文件... [完毕]"
}

function total_mainproc() 
{
	if [ "x$1" = "xbuild" ]; then
		total_build_module
	elif [ "x$1" = "xclean" ]; then
    	total_clean_module
	elif [ "x$1" = "xpack" ]; then
		total_pack_module
	elif [ "x$1" = "xinstall" ]; then
		total_install_module
	else
		sinfo "Usage:"
		sinfo " $0 build   批量编译midkit程序"
		sinfo " $0 clean   批量清除midkit程序"
		sinfo " $0 pack    批量备份midkit程序"
		sinfo " $0 install 批量安装到z.src.rtfs"
	fi
}
total_mainproc $1
